<template>
  <el-form ref="refForm" :model="user" :rules="rules">
    <el-form-item label="姓名" prop="name">
      <el-input v-model.trim="user.name" type="text" clearable />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model.trim="user.phone" type="text" clearable />
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <el-input v-model.trim="user.avatar" type="text" clearable />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">确认修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserSelf } from '@/api/system/user'

export default {
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          id: 0,
          name: '',
          phone: '',
          avatar: ''
        }
      }
    }
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    submit() {
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          updateUserSelf({
            id: this.user.id,
            name: this.user.name,
            phone: this.user.phone,
            avatar: this.user.avatar
          }).then(res => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.formVisible = false
          }).then(res => {})
        }
      })
    }
  }
}
</script>
